package com.xiangban.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.dao.HospitalMapper;
import com.xiangban.exception.BizException;
import com.xiangban.model.Hospital;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.model.res.Ids;
import com.xiangban.service.HospitalService;
import com.xiangban.utils.CommonUtils;
import com.xiangban.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.xiangban.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/12/1 9:39
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;

    @Override
    public void saveHospital(Hospital hospital) {
        boolean flag = checkHospitalParameter(hospital);
        if (!flag){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        hospital.setStatus(0);
        hospital.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        hospitalMapper.insertHospital(hospital);
    }

    public boolean checkHospitalParameter(Hospital hospital) {
        if (BizEmpty(hospital.getName())){
            return false;
        }
        if (BizEmpty(hospital.getAddress())){
            return false;
        }
        if (BizEmpty(hospital.getLongitude())){
            return false;
        }
        if (BizEmpty(hospital.getLatitude())){
            return false;
        }
        if (BizEmpty(hospital.getType())){
            return false;
        }
        if (BizEmpty(hospital.getSort())){
            return false;
        }
        if (BizEmpty(hospital.getImage())){
            return false;
        }
        if (BizEmpty(hospital.getPhone())){
            return false;
        }
        return true;
    }

    @Override
    public void removeHospital(Ids ids) {
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        hospitalMapper.deleteHospital(ids);
    }

    @Override
    public void updateHospital(Hospital hospital) {
        if (BizEmpty(hospital.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        hospitalMapper.updateHospital(hospital);
    }

    @Override
    public PageInfo<Hospital> listHospitalPage(Hospital hospital) {
        hospital.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<Hospital> hospitals = hospitalMapper.listHospitalPage(hospital);
        return new PageInfo<>(hospitals);
    }

    @Override
    public Object getHospital(Hospital hospital) {
        if (BizEmpty(hospital.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        return hospitalMapper.getHospital(hospital);
    }


}
